﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.PageView.EmpBasicManager
{
    public partial class EmpDegreeWindow : ChildWindow
    {
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private EmpbasicService.Emp_DegreeModel model = new EmpbasicService.Emp_DegreeModel();
        private EmpbasicService.Emp_DegreeModel oldmodel = new EmpbasicService.Emp_DegreeModel();
        private InsertSystemRemark insertsys = new InsertSystemRemark();
        public int ID;
        public int EmpID;
        public bool IsNew = false;
        public EmpDegreeWindow(int empID, int iD, bool isnew)
        {
            InitializeComponent();
            ID = iD;
            EmpID = empID;
            IsNew = isnew;
            if (IsNew)
            {
                this.Title = "新增学位信息";
                model = new EmpbasicService.Emp_DegreeModel();
                this.DataContext = model;
            }
            else
            {
                this.Title = "编辑学位信息";
                Client.GetEmp_DegreeModelCompleted += new EventHandler<EmpbasicService.GetEmp_DegreeModelCompletedEventArgs>(Client_GetEmp_DegreeModelCompleted);
                Client.GetEmp_DegreeModelAsync("(" + EmpID + ")", ID);
            }
        }

        void Client_GetEmp_DegreeModelCompleted(object sender, EmpbasicService.GetEmp_DegreeModelCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                model = e.Result[0];
                oldmodel = new EmpbasicService.Emp_DegreeModel
                {
                    DegreeName = model.DegreeName,
                    LearningType = model.LearningType,
                    EducationalSystem = model.EducationalSystem,
                    EducationDate = model.EducationDate,
                    School = model.School,
                    Professional = model.Professional
                };
            }
            this.DataContext = model;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (IsNew == true)
            {
                model.EmpBasicID = EmpID;

                Client.AddEmp_DegreeModelCompleted += new EventHandler<EmpbasicService.AddEmp_DegreeModelCompletedEventArgs>(Client_AddEmp_DegreeModelCompleted);
                Client.AddEmp_DegreeModelAsync(model);
            }
            else
            {
                if (!string.IsNullOrEmpty(begin.Text)) model.EducationDate = Convert.ToDateTime(begin.Text);
                else
                    model.EducationDate = null;
                Client.UpdateEmp_DegreeModelCompleted += new EventHandler<EmpbasicService.UpdateEmp_DegreeModelCompletedEventArgs>(Client_UpdateEmp_DegreeModelCompleted);
                Client.UpdateEmp_DegreeModelAsync(model);
            }
        }

        void Client_UpdateEmp_DegreeModelCompleted(object sender, EmpbasicService.UpdateEmp_DegreeModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
                InsertSystem("编辑");
                this.DialogResult = true;
            }
        }

        void Client_AddEmp_DegreeModelCompleted(object sender, EmpbasicService.AddEmp_DegreeModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
                InsertSystem("新增");
                this.DialogResult = true;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        void InsertSystem(string FangShi)
        {
            if (FangShi == "新增")
            {
                insertsys.Insert("学位子集", "", "", model.EmpBasicID, FangShi, "");
            }
            else
            {
                if (oldmodel.EducationDate != model.EducationDate)
                    insertsys.Insert("学位子集", oldmodel.EducationDate == null ? null : Convert.ToDateTime(oldmodel.EducationDate).ToShortDateString(),
                        model.EducationDate == null ? null : Convert.ToDateTime(model.EducationDate).ToShortDateString(),
                        model.EmpBasicID, FangShi, "毕（肄）业日期");
                if (oldmodel.DegreeName != model.DegreeName)
                    insertsys.Insert("学位子集", oldmodel.DegreeName, model.DegreeName,
                        model.EmpBasicID, FangShi, "学位名称");
                if (oldmodel.LearningType != model.LearningType)
                    insertsys.Insert("学位子集", oldmodel.LearningType, model.LearningType,
                        model.EmpBasicID, FangShi, "学习形式");

                if (oldmodel.EducationalSystem != model.EducationalSystem)
                    insertsys.Insert("学位子集", oldmodel.EducationalSystem, model.EducationalSystem,
                        model.EmpBasicID, FangShi, "学制");
                if (oldmodel.School != model.School)
                    insertsys.Insert("学位子集", oldmodel.School, model.School,
                        model.EmpBasicID, FangShi, "学校");
                if (oldmodel.Professional != model.Professional)
                    insertsys.Insert("学位子集", oldmodel.Professional, model.Professional,
                        model.EmpBasicID, FangShi, "所学专业");
            }

        }
    }
}
